# Contributor: Fabricio Silva <hi@fabricio.dev>
# Maintainer: Fabricio Silva <hi@fabricio.dev>
pkgname=adguardhome
pkgver=0.107.53
pkgrel=0
pkgdesc="Network-wide ads & trackers blocking DNS server"
url="https://adguard.com/adguard-home.html"
arch="all"
license="GPL-3.0-only"
options="net setcap" # net for go
depends="ca-certificates"
makedepends="go libcap-utils"
checkdepends="tzdata"
subpackages="$pkgname-openrc"
install="$pkgname.pre-install"
source="
	$pkgname-$pkgver.tar.gz::https://github.com/AdguardTeam/AdGuardHome/archive/v$pkgver.tar.gz
	$pkgname-frontend-$pkgver.tar.gz::https://github.com/AdguardTeam/AdGuardHome/releases/download/v$pkgver/AdGuardHome_frontend.tar.gz
	adguardhome.initd
	adguardhome.confd
	cap_check.patch
	go_version.patch
	"
builddir="$srcdir/AdGuardHome-$pkgver"
pkgusers="adguardhome"
pkggroups="adguardhome"

export GOCACHE="${GOCACHE:-"$srcdir/go-cache"}"
export GOTMPDIR="${GOTMPDIR:-"$srcdir"}"
export GOMODCACHE="${GOMODCACHE:-"$srcdir/go"}"

prepare() {
	default_prepare

	# frontend pre-built files (symlink dont work)
	rm -rf build/static
	cp -al "$srcdir"/build/static build/static

	go mod download
}

build() {
	go build -o bin/adguardhome -trimpath -ldflags "-s -w
		-X github.com/AdguardTeam/AdGuardHome/internal/version.version=v$pkgver
		-X github.com/AdguardTeam/AdGuardHome/internal/version.channel=release
		-X github.com/AdguardTeam/AdGuardHome/internal/version.committime=${SOURCE_DATE_EPOCH:-$(date +%s)}
		"
}

check() {
	go test ./...
}

package() {
	install -Dm754 -o $pkgusers -g $pkggroups bin/adguardhome -t "$pkgdir"/usr/bin/
	# https://adguard-dns.io/kb/adguard-home/getting-started/#running-without-superuser
	# bind_service for ports lower than 1024 / raw for dhcp/sockets
	setcap "cap_net_bind_service+eip cap_net_raw+eip" "$pkgdir"/usr/bin/adguardhome

	install -Dm755 "$srcdir"/$pkgname.initd "$pkgdir"/etc/init.d/$pkgname
	install -Dm644 "$srcdir"/$pkgname.confd "$pkgdir"/etc/conf.d/$pkgname
}

sha512sums="
f13eeb83d00ff88854e0509b2c3f396ec25d7cc19e855e1802ebb2dab97de5d8eda8945186e8889257fb566bb7ff5f27300e7c719f182dd4e1f13fdd7b881a91  adguardhome-0.107.53.tar.gz
f146d2b6974c54503a98516930749037c853c5d634772187abd13fb001f81d5f5f7c6448424ce629b34fc6202d507d693a4f5ac134419a762e69323e194f0efc  adguardhome-frontend-0.107.53.tar.gz
566ef136e5c8ab0a7bc067e393b1cb50068140c205f4d7392cc8a5be399b505b3d71ff60708302264b63a11a08d9f85fdf9bf1cf570c664910d149353116313c  adguardhome.initd
f7b7365079b901cbc08c4d0553160a7e5f15205b509e8dc867351338153820acb705807763c12cd66f5120dfa1c008c04a2f01c60331951146f4938791dfea32  adguardhome.confd
b0dde70e9fc1ee40916233d884de2d8fae1a29b6036160334497b02ba7061e009b53b98cc49aae5dfde93d6226657a73ef833e2452e91a41f54bf74fdb8dbcb3  cap_check.patch
612abe5deffc010d8c093a1fe6fcbceea7dc117ae23a83c17ca7b44ff7817e33d546ea6e35e7045d2ca87e6ef4d0772c80797b1c39497b26e2321b38a885008c  go_version.patch
"
